// 发起请求
$('.btn').click(function () {
    const email = $('.email input').val()
    const password = $('.password input').val()

    if (!email) {
        myAlert(false, '邮箱不能为空')
        return false;
    }

    if (!password) {
        myAlert(false, '密码不能为空')
        return false;
    }

    const reg = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
    if (!reg.test(email)) {
        myAlert(false, '邮箱格式不正确')
        return false;
    }

    if (password.length < 6) {
        myAlert(false, '密码长度不能小于6位')
        return false;
    }

    loginRequest({
        email,
        password
    })

})

// 请求
function loginRequest(data = []) {
    $.ajax({
        url: 'http://localhost:8081/api/login.php',
        type: 'post',
        data: data
    }).then(res => {
        if (res.code === 1) {
            myAlert(true, '登录成功')
            $('.form')[0].reset()
            localStorage.setItem('userInfo', JSON.stringify(res.data))
            localStorage.setItem('token', res.token)
            setTimeout(() => {
                location.href = '../../view/layout/layout.php'
            }, 500)
        } else {
            myAlert(false, res.msg)
        }
    })
}